//
//  ChatViewController.m
//  qq
//
//  Created by 王成龙 on 16/10/18.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "ChatViewController.h"
#import "ListData.h"

@interface ChatViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray<ListItemData *>*_data;
}
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *act;



@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
}

-(void)loadData
{
    [_act startAnimating];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/list.php"]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSString *text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        ListData *li = [[ListData alloc]initWithDictionary:dict];
        dispatch_async(dispatch_get_main_queue(), ^{
            if(li.errcode == 0)
            {
                [_act startAnimating];
                _data = li.record;
                [self.tableview reloadData];
            }else
            {
                NSLog(@"%@",li.errinfo);
            }
        
        });
    
    }];
    [task resume];

     }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    ListItemData * list = _data[indexPath.row];
    cell.textLabel.text = list.name;
    cell.detailTextLabel.text = list.tel;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
